package com.pbs.ui.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.pbs.net.GMailSender;
import com.pbs.net.WSF;
import com.pbs.ui.R;
import com.pbs.utils.EmailParser;

public class RestoreAccountDialog extends Dialog implements OnClickListener {

	public RestoreAccountDialog(Context context) {
		super(context, R.style.Dialog);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.restore_account, null);
		((Button) layout.findViewById(R.id.restoreAccountButton))
				.setOnClickListener(this);
		((Button) layout.findViewById(R.id.cancelButton))
				.setOnClickListener(this);
		
		setContentView(layout);
	}
	
	private AlertDialog showMessageDialog(String title, String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
		builder.setIcon(R.drawable.info_icon);
		if (title == null) {
			return builder.setMessage(msg).show();
		} else {
			return builder.setMessage(msg).setTitle(title).show();
		}
	}
	
	private ProgressDialog showProgressDialog(String msg, boolean cancelable) {
		ProgressDialog pd = new ProgressDialog(getContext());
		if (msg != null)
			pd.setMessage(msg);
		pd.setIcon(R.drawable.process_icon);
		pd.setCancelable(cancelable);
		pd.show();
		return pd;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.restoreAccountButton:{
			final String email = ((EditText)findViewById(R.id.txtEmail)).getText().toString();
			if (new EmailParser(email).isValid()) {
				final ProgressDialog pd = showProgressDialog("Account is sending to your email...", false);
				new Thread() {
					public void run() {
						try {
							String result = WSF.getAccountConnectedToEmail(email);
							if (result.equals("")) {
								new Handler().post(new Runnable() {
									@Override
									public void run() {
										showMessageDialog(null, "We cannot found any usernames connected with your email \"" + email + "\"");
									}
								});
							} else {
								GMailSender sender = new GMailSender(getContext().getString(R.string.mail_username), getContext().getString(R.string.mail_password));
								sender.sendMail("Support recover username from phonebooksafe", "Your username: " + result, "account-recovery-noreply", email);
								new Handler().post(new Runnable() {
									@Override
									public void run() {
										showMessageDialog(null, "Your account sent to \"" + email + "\"");
									}
								});
							}
							new Handler().post(new Runnable() {
								@Override
								public void run() {
									pd.dismiss();
								}
							});

						} catch (final Exception e) {
							new Handler().post(new Runnable() {
								@Override
								public void run() {
									pd.dismiss();
									showMessageDialog("Error Connect", e.getMessage() + "\nPlease try again");
								}
							});
						}
					}
				}.start();
			} else {
				Toast.makeText(getContext(), R.string.msg_EmailInvalid, Toast.LENGTH_SHORT).show();
			}
			break;
		}
		case R.id.cancelButton:{
			dismiss();
		}
		}
	}
}
